/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cstdio>
#include <unistd.h>

#include <gtest/gtest.h>
#include <smart_serve_client.h>

#include <nlohmann/json.hpp>

namespace OHOS {
namespace SmartServe {

using namespace testing;
using namespace testing::ext;
using json = nlohmann::json;

class SmartServeTest : public testing::Test {
public:
    static void SetUpTestCase();
    static void TearDownTestCase();
    void SetUp();
    void TearDown();
};

void SmartServeTest::SetUpTestCase()
{
}

void SmartServeTest::TearDownTestCase()
{
}

void SmartServeTest::SetUp()
{
}

void SmartServeTest::TearDown()
{
}

template<typename Object>
std::string Dump(const Object& obj)
{
    return json(obj).dump();
}

auto MakeEvalLLMRequest(const std::string& model, const std::string& input)
{
    json request = {
        {"model", model},
        {"input", input},
    };
    return request.dump();
}

auto MakeGetLLMResultRequest(uint64_t id)
{
    json request = {
        {"id", id},
    };
    return request.dump();
}

static std::string GetResponse(const std::string &response, std::string &error)
{
    std::string content = "";
    try {
        json j = json::parse(response);
        content = j["content"];
        error = j["error"];
    } catch(const json::parse_error &e) {
        error = "";
    }
    return content;
}

static int64_t GetTaskid(const std::string &result)
{
    int64_t id = -1;
    try {
        json data = json::parse(result);
        id = data["id"];
    } catch(const json::parse_error &e) {
        id = -1;
    }
    return id;
}

/**
 * @tc.name: SmartServeInterfaceTest
 * @tc.desc: Verify the interface of smart serve.
 * @tc.type: FUNC
 */
HWTEST_F(SmartServeTest, SmartServeInterfaceTest, TestSize.Level0)
{
    std::string model = "qwen3-0.6b";
    std::string text = "<|im_start|>user\nHello, Who are you?<|im_end|>\n<|im_start|>assistant\n";
    std::string result = SmartServeClient::GetInstance().Serve(MakeEvalLLMRequest(model, text));
    int64_t id = GetTaskid(result);
    if (id <= 0) {
        std::cout << "Failed to send request by Serve interface" << std::endl;
        EXPECT_GT(id, 0);
    }
    std::string error = "";
    std::string response = "";
    while (error == "") {
        result = SmartServeClient::GetInstance().Serve(MakeGetLLMResultRequest(id));
        response += GetResponse(result, error);
    }
    std::cout << "Serve response:" << std::endl << response << std::endl;
}

} // namespace SmartServe
} // namespace OHOS
